package com.fuhailiu.opengl.activities;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fuhailiu.opengl.R;
import com.fuhailiu.opengl.config.ItemData;
import com.fuhailiu.opengl.config.ItemDataManager;
import com.fuhailiu.opengl.define.AppGlobalDef;
import com.fuhailiu.opengl.utils.LogUtil;
import com.fuhailiu.opengl.utils.ResourceUtil;

public class MainActivity extends ListActivity {

	private final String TAG = getClass().getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		LogUtil.LogD(TAG, "onCreate   <---");

		super.onCreate(savedInstanceState);
		Adapter adapter = new Adapter(this);
		getListView().setAdapter(adapter);
		getListView().setOnItemClickListener(adapter);

		LogUtil.LogD(TAG, "onCreate   --->");
	}

	@Override
	protected void onResume() {
		LogUtil.LogD(TAG, "onResume   <---");
		super.onResume();
		LogUtil.LogD(TAG, "onResume   --->");
	}

	@Override
	protected void onPause() {
		LogUtil.LogD(TAG, "onPause   <---");
		super.onPause();
		LogUtil.LogD(TAG, "onPause   --->");
	}

	@Override
	protected void onDestroy() {
		LogUtil.LogD(TAG, "onDestroy   <---");
		super.onDestroy();
		LogUtil.LogD(TAG, "onDestroy   --->");
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		LogUtil.LogD(TAG, "onConfigurationChanged   <---");
		super.onConfigurationChanged(newConfig);
		LogUtil.LogD(TAG, "onConfigurationChanged   --->");
	}

	private final class Adapter extends BaseAdapter implements OnItemClickListener {

		private final class ViewHolder {
			private TextView mTextView;
		}

		private Context mContext;
		private ArrayList<ItemData> mItemDataList;

		public Adapter(Context context) {
			mContext = context;
			mItemDataList = ItemDataManager.getItemDataList();
		}

		@Override
		public int getCount() {
			return mItemDataList.size();
		}

		@Override
		public Object getItem(int position) {
			return mItemDataList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;

			if (null == convertView) {
				convertView = getLayoutInflater().inflate(R.layout.list_item, null);
				holder = new ViewHolder();
				holder.mTextView = (TextView) convertView.findViewById(R.id.tv_list_item);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			ItemData data = (ItemData) getItem(position);
			holder.mTextView.setText(ResourceUtil.getString(mContext, data.resId));

			return convertView;
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
			ItemData data = (ItemData) getItem(position);
			Intent intent = new Intent(MainActivity.this, OpenGLActivity.class);
			intent.putExtra(AppGlobalDef.INTENT_KEY_RENDER, data.renderId);
			startActivity(intent);
		}

	}

}
